//
//  GMNoteCell.m
//  Teecloud
//
//  Created by 小熊 on 2018/10/22.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "GMNoteCell.h"
#import "TZImagePickerController.h"
#import <KSPhotoBrowser/KSPhotoBrowser.h>
#import "GMPhotoCell.h"
#import "GMPlayerVoideCell.h"
#import "GMAttachListModel.h"
#import "GMLookPDFVC.h"
@interface GMNoteCell ()<UICollectionViewDataSource,UICollectionViewDelegate,TZImagePickerControllerDelegate,UICollectionViewDelegateFlowLayout,UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *headImg;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *timeLab;
@property (weak, nonatomic) IBOutlet UILabel *content;
@property (weak, nonatomic) IBOutlet UITableView *videoView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *videoHight;
@property (weak, nonatomic) IBOutlet UICollectionView *imgsView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgHight;
/**播放时间视图*/
@property (weak, nonatomic) IBOutlet UIView *stateView;
@property (weak, nonatomic) IBOutlet UILabel *postionTime;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *stateHight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bagViewHight;

@end
@implementation GMNoteCell

- (void)awakeFromNib {
    [super awakeFromNib];
    _imgsView.backgroundColor = kWhiteColor;
    _imgsView.dataSource = self;
    _imgsView.scrollEnabled = NO;
    _imgsView.delegate = self;
    [_imgsView registerNib:[UINib nibWithNibName:@"GMPhotoCell" bundle:nil] forCellWithReuseIdentifier:@"GMPhotoCell"];
    _videoView.dataSource = self;
    _videoView.delegate = self;
    _videoView.scrollEnabled = NO;
    [_videoView registerNib:[UINib nibWithNibName:@"GMPlayerVoideCell" bundle:nil] forCellReuseIdentifier:@"GMPlayerVoideCell"];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doTapChange:)];
    [self.stateView addGestureRecognizer:tap];
}
//点击
-(void)doTapChange:(UITapGestureRecognizer *)sender{
    self.playTap();
    
}
-(void)setModel:(GMNoteModel *)model{
    _model = model;
    [self.headImg yy_setImageWithURL:[NSURL URLWithString:model.HeadUrl] placeholder:DefealtImg];
    self.name.text = model.Name;
    self.timeLab.text = model.NoteTime;
    self.content.text = model.Content;
    CGFloat contentHight = [self.content getLableSizeWithMaxWidth:(DEVICE_WIDTH-58)].height;
    if (model.VoiceList.count ==0) {
        self.videoView.hidden =YES;
        self.videoHight.constant = 0;
    }else{
        self.videoView.hidden =NO;
        self.videoHight.constant = 30*model.VoiceList.count;
        [self.videoView reloadData];
    }
    if (model.ImgFileList.count ==0) {
        self.imgsView.hidden = YES;
        self.imgHight.constant = 0;
    }else{
        self.imgsView.hidden = NO;
        self.imgHight.constant = 60;
        [self.imgsView reloadData];
    }
    if (model.Position) {
        self.stateView.hidden = NO;
        self.stateHight.constant = 40;
        self.postionTime.text = model.PositionStr;
    }else{
        self.stateView.hidden = YES;
        self.stateHight.constant = 0;
    }
    self.bagViewHight.constant = 60+2+contentHight +2+self.videoHight.constant+2 +self.imgHight.constant+2+self.stateHight.constant;
    model.cellHight = self.bagViewHight.constant +1;
}
//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.ImgFileList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GMPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GMPhotoCell" forIndexPath:indexPath];
    GMAttachListModel *model= self.model.ImgFileList[indexPath.row];
    NSLog(@"%@",model.FileType);
    if ([model.FileType isEqualToNumber:@(3)]) {
        [cell.photoImg yy_setImageWithURL:[NSURL URLWithString:model.Url] placeholder:DefealtImg];
    }else if ([model.FileType isEqualToNumber:@(4)]){
        cell.photoImg.image =[UIImage imageNamed:@"doc-pdf"];
    }else if ([model.FileType isEqualToNumber:@(5)]){
        cell.photoImg.image =[UIImage imageNamed:@"doc-ppt"];
    }else if ([model.FileType isEqualToNumber:@(6)]){
        cell.photoImg.image =[UIImage imageNamed:@"doc-word"];
    }else if ([model.FileType isEqualToNumber:@(7)]){
        cell.photoImg.image =[UIImage imageNamed:@"doc-excel"];
    }else if ([model.FileType isEqualToNumber:@(8)]){
        cell.photoImg.image =[UIImage imageNamed:@"doc-rar"];
    }else if ([model.FileType isEqualToNumber:@(9)]){
        cell.photoImg.image =[UIImage imageNamed:@"doc-zip"];
    }else{
        cell.photoImg.image =[UIImage imageNamed:@"doc-other"];
    }
    cell.delectBtn.hidden=YES;
    return cell;
}
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((DEVICE_WIDTH-88-10-10-40)/5, (DEVICE_WIDTH-88-10-10-40)/5);
}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 0, 10,4);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}
//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}
//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    /**放大图片*/
    GMAttachListModel *fileModel= self.model.ImgFileList[indexPath.row];
    if ([fileModel.FileType isEqualToNumber:@(3)]) {
        GMPhotoCell * cell = (GMPhotoCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];//即为要得到的cell
        NSMutableArray *items = @[].mutableCopy;
        NSInteger count = self.model.ImgFileList.count;
        for (int i = 0; i < count; i++) {
            GMAttachListModel *model= self.model.ImgFileList[i];
            if ([model.FileType isEqualToNumber:@(3)]) {
                UIImageView *imgV = cell.photoImg;
                KSPhotoItem *item = [KSPhotoItem itemWithSourceView:imgV imageUrl:[NSURL URLWithString:model.Url]];
                [items addObject:item];
            }
        }
        KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:indexPath.row];
        browser.dismissalStyle = KSPhotoBrowserInteractiveDismissalStyleRotation;
        browser.backgroundStyle = KSPhotoBrowserBackgroundStyleBlurPhoto;
        browser.loadingStyle = KSPhotoBrowserImageLoadingStyleDeterminate;
        browser.pageindicatorStyle = KSPhotoBrowserPageIndicatorStyleDot;
        browser.bounces = NO;
        [browser showFromViewController:self.baseVC];
    }else{
        GMLookPDFVC *preview = [[GMLookPDFVC alloc] init];
        preview.model = fileModel;
        [self.baseVC.navigationController pushViewController:preview animated:YES];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.model.VoiceList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GMPlayerVoideCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GMPlayerVoideCell" forIndexPath:indexPath];
    GMVoiceListModel *model = self.model.VoiceList[indexPath.row];
    cell.model = model;
    kWeakObject(self)
    cell.readBlock = ^{
        weakself.hasReadBlock(indexPath.row, model.Id);
    };
    return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

@end
